package cn.ctyun.videoplayer.controller;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import cn.ctyun.videoplayer.R;
import cn.ctyun.videoplayer.controller.BaseVideoController;
import cn.ctyun.videoplayer.controller.BaseMediaPlayerControlInterface;
import cn.ctyun.videoplayer.listener.AdControllerListener;
import cn.ctyun.videoplayer.player.IjkVideoView;
import cn.ctyun.videoplayer.util.PlayerUtils;

/**
 * 广告控制器
 * Created by Devlin_n on 2017/4/12.
 */

public class AdController extends BaseVideoController implements View.OnClickListener {
    protected TextView adTime, adDetail;
    protected ImageView back, volume, fullScreen;
    protected AdControllerListener listener;
    protected ImageView mStartPlayButton;
    protected RelativeLayout mTopContainer;
    protected LinearLayout mBottomContainer;
    private ProgressBar mLoadingProgress;
    private ImageView mThumb;
    private LinearLayout mCompleteContainer;

    public AdController(@NonNull Context context) {
        super(context);
    }

    public AdController(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_ad_controller;
    }

    @Override
    protected void initView() {
        super.initView();
        mBottomContainer = mControllerView.findViewById(R.id.bottom_container);
        mTopContainer = mControllerView.findViewById(R.id.top_container);
        mStartPlayButton = mControllerView.findViewById(R.id.start_play);
        mLoadingProgress = mControllerView.findViewById(R.id.loading);
        mCompleteContainer = mControllerView.findViewById(R.id.complete_container);
        ImageView rePlayButton = mControllerView.findViewById(R.id.iv_replay);
        rePlayButton.setOnClickListener(this);
        mThumb = mControllerView.findViewById(R.id.thumb);
        mStartPlayButton.setOnClickListener(this);
        adTime = mControllerView.findViewById(R.id.ad_time);
        adDetail = mControllerView.findViewById(R.id.ad_detail);
        adDetail.setText("了解详情>");
        back = mControllerView.findViewById(R.id.back);
//        back.setVisibility(GONE);
        volume = mControllerView.findViewById(R.id.iv_volume);
        fullScreen = mControllerView.findViewById(R.id.fullscreen);
//        playButton = mControllerView.findViewById(R.id.iv_play);
//        playButton.setOnClickListener(this);
        adTime.setOnClickListener(this);
        adDetail.setOnClickListener(this);
        back.setOnClickListener(this);
        volume.setOnClickListener(this);
        fullScreen.setOnClickListener(this);
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) listener.onAdClick();
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.fullscreen) {
            doStartStopFullScreen();
        } else if (id == R.id.back) {
            mAttachActivity.onBackPressed();
        } else if (id == R.id.iv_volume) {
            doMute();
        } else if (id == R.id.ad_detail) {
            if (listener != null) listener.onAdClick();
        } else if (id == R.id.ad_time) {
            if (listener != null) listener.onCancelAdClick();
        } else if (id == R.id.start_play) {
            doPauseResume();
        } else if (id == R.id.iv_replay) {
//            ((MediaPlayerControlInterface)mMediaPlayer).restart();
            mMediaPlayer.retry();
        }
    }

    private void doMute() {
        mMediaPlayer.setMute(!mMediaPlayer.isMute());
        volume.setImageResource(mMediaPlayer.isMute() ? R.drawable.ic_action_volume_off : R.drawable.ic_action_volume_up);
    }

    @Override
    public void setPlayState(int playState) {
        super.setPlayState(playState);
//        if (IjkVideoView.STATE_IDLE == playState) {
//            mStartPlayButton.setVisibility(VISIBLE);
//            mTopContainer.setVisibility(GONE);
//            mBottomContainer.setVisibility(GONE);
//        } else {
//            mStartPlayButton.setVisibility(GONE);
//            mTopContainer.setVisibility(VISIBLE);
//            mBottomContainer.setVisibility(VISIBLE);
//        }
//        if (IjkVideoView.STATE_PREPARING  == playState || IjkVideoView.STATE_BUFFERING == playState) {
//            mLoadingProgress.setVisibility(VISIBLE);
//        } else {
//            mLoadingProgress.setVisibility(GONE);
//        }

//        switch (playState) {
//            case IjkVideoView.STATE_PLAYING:
//                post(mShowProgress);
//                playButton.setSelected(true);
//                break;
//            case IjkVideoView.STATE_PAUSED:
//                playButton.setSelected(false);
//                break;
//        }
//        Log.d("ADController", "playState = " + playState);
        switch (playState) {
            case IjkVideoView.STATE_IDLE:
                hide();
                mIsLocked = false;
                mMediaPlayer.setLock(false);
                mLoadingProgress.setVisibility(GONE);
                mStartPlayButton.setVisibility(VISIBLE);
                mThumb.setVisibility(VISIBLE);
                mTopContainer.setVisibility(GONE);
                mBottomContainer.setVisibility(GONE);
                mCompleteContainer.setVisibility(GONE);
                break;
            case IjkVideoView.STATE_PLAYING:
                post(mShowProgress);
//                playButton.setSelected(true);
                mThumb.setVisibility(GONE);
                mLoadingProgress.setVisibility(GONE);
                mStartPlayButton.setVisibility(GONE);
                mTopContainer.setVisibility(VISIBLE);
                mBottomContainer.setVisibility(VISIBLE);
                mCompleteContainer.setVisibility(GONE);
                break;
            case IjkVideoView.STATE_PAUSED:
//                playButton.setSelected(false);
                mStartPlayButton.setVisibility(GONE);
                mTopContainer.setVisibility(VISIBLE);
                mBottomContainer.setVisibility(VISIBLE);
                break;
            case IjkVideoView.STATE_PREPARING:
                mStartPlayButton.setVisibility(GONE);
                mLoadingProgress.setVisibility(VISIBLE);
//                mTopContainer.setVisibility(VISIBLE);
//                mBottomContainer.setVisibility(VISIBLE);
                mCompleteContainer.setVisibility(GONE);
                break;
            case IjkVideoView.STATE_PREPARED:
                mStartPlayButton.setVisibility(GONE);
                mTopContainer.setVisibility(VISIBLE);
                mBottomContainer.setVisibility(VISIBLE);
                break;
            case IjkVideoView.STATE_ERROR:
                mStartPlayButton.setVisibility(GONE);
                mLoadingProgress.setVisibility(GONE);
                mThumb.setVisibility(GONE);
                mTopContainer.setVisibility(GONE);
                mBottomContainer.setVisibility(GONE);
                break;
            case IjkVideoView.STATE_BUFFERING:
                mStartPlayButton.setVisibility(GONE);
                mLoadingProgress.setVisibility(VISIBLE);
                mThumb.setVisibility(GONE);
                mTopContainer.setVisibility(VISIBLE);
                mBottomContainer.setVisibility(VISIBLE);
                break;
            case IjkVideoView.STATE_BUFFERED:
                mLoadingProgress.setVisibility(GONE);
                mStartPlayButton.setVisibility(GONE);
                mThumb.setVisibility(GONE);
                mTopContainer.setVisibility(VISIBLE);
                mBottomContainer.setVisibility(VISIBLE);
                break;
            case IjkVideoView.STATE_PLAYBACK_COMPLETED:
                mStartPlayButton.setVisibility(GONE);
                mThumb.setVisibility(VISIBLE);
                mIsLocked = false;
                mMediaPlayer.setLock(false);
                mTopContainer.setVisibility(GONE);
                mBottomContainer.setVisibility(GONE);
                mCompleteContainer.setVisibility(VISIBLE);
                break;
        }
    }

    @Override
    public void setPlayerState(int playerState) {
        super.setPlayerState(playerState);
        switch (playerState) {
            case IjkVideoView.PLAYER_NORMAL:
//                back.setVisibility(GONE);
//                fullScreen.setSelected(false);
                fullScreen.setVisibility(VISIBLE);
                break;
            case IjkVideoView.PLAYER_FULL_SCREEN:
//                back.setVisibility(VISIBLE);
//                fullScreen.setSelected(true);
                fullScreen.setVisibility(GONE);
                break;
        }
    }

    @Override
    protected int setProgress() {
        if (mMediaPlayer == null) {
            return 0;
        }
        int position = (int) mMediaPlayer.getCurrentPosition();
        int duration = (int) mMediaPlayer.getDuration();

        if (adTime != null) {
            adTime.setText(String.format("%s | 关闭广告", (duration - position) / 1000));
        }
        return position;
    }

    @Override
    public boolean onBackPressed() {
        if (mMediaPlayer.isFullScreen()) {
            PlayerUtils.scanForActivity(getContext()).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            mMediaPlayer.stopFullScreen();
            setPlayerState(IjkVideoView.PLAYER_NORMAL);
            return true;
        }
        clearStatus();
        return super.onBackPressed();
    }

    private void clearStatus() {
        if (adTime != null) {
            adTime.setText("");
        }
    }

    public void setControllerListener(AdControllerListener listener) {
        this.listener = listener;
    }

    @Override
    public void setMediaPlayer(BaseMediaPlayerControlInterface mediaPlayer) {
        super.setMediaPlayer(mediaPlayer);
        if (mediaPlayer != null) {
            if (!TextUtils.isEmpty(mThumbUrl)) {
                Glide.with(mAttachActivity).load(mThumbUrl).into(mThumb);
            }
        }
    }

    /**
     * 设置是否为首次播放
     */
    public void setFirstPlay(boolean firstPlay) {
        if (firstPlay) {
            mStartPlayButton.setVisibility(VISIBLE);
        } else {
            mStartPlayButton.setVisibility(GONE);
        }
    }
}
